#include<iostream>
#include<unistd.h>
#include<fcntl.h>
#include<string.h>
#include<cstdio>
#include<cerrno>
#include<cstdlib>
using namespace std;


void SetNonBlock(int fd)
{
    int f=fcntl(fd,F_GETFL);
    if(f<0)
    {
        cerr<<"error"<<endl;
        return ;
    }
    fcntl(fd,F_SETFL, f | O_NONBLOCK);
    cout<<"set"<<fd<<"read done"<<endl;
}
int main()
{
    char buffer[1024];
    SetNonBlock(0);
    while(true)
    {

        //cout<<"please #:"<<endl;
        ssize_t n=read(0,buffer,sizeof(buffer)-1);
        if(n>0)
        {
            buffer[n]=0;
            cout<<"echo : "<<buffer<<endl;
        }
        else if(n==0)
        {
            cout<<"read done"<<endl;
        }
        else{
            if(errno==EWOULDBLOCK)
            {
                cout<<" try again!"<<endl;
            }
            else
            {
                cout<<"read done,n= "<<n<<"errno: "<<errno<<",error str:"<<strerror(errno)<<endl;

            }
            
        }
    }
}